import React from "react";
import { Button, Checkbox, Form, Input } from 'antd';

import { connect } from 'react-redux'
import request from "../utils/request";
import { useNavigate } from 'react-router-dom'
function Login(props) {
    // console.log(login);
    // console.log('props', props);
    const navigate = useNavigate()
    const onFinish = async (values) => {
        const loginMs = await request('/user/login', {
            params: values
        })
        console.log(loginMs);
        // console.log('Success:', values);
        console.log('loginMs', loginMs);
        if (loginMs.data.code === 200) {
            props.login({ ...loginMs.data, username: values.username })
            navigate('/manage')
        }
    };


    // console.log('props', props);
    return (
        <div className="login">
            <Form
                name="basic"
                labelCol={{
                    span: 8,
                }}
                wrapperCol={{
                    span: 16,
                }}
                initialValues={{
                    remember: true,
                }}
                onFinish={onFinish}
                // onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="username"
                    rules={[
                        {
                            required: true,
                            message: '输入账号',
                        },
                    ]}
                    wrapperCol={{ span: 10 }}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: '输入密码',
                        },
                    ]}
                    wrapperCol={{ span: 10 }}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 10,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

const mapStateToProps = function (state) {
    console.log('state', state);
    return {
        user: state
    }
}

// 把修改redux的方法映射到props
const mapDispatchToProps = function (dispatch) {
    return {
        login(userInfo) {
            dispatch({ type: 'login', payload: userInfo })
        }
    }
}
Login = connect(mapStateToProps, mapDispatchToProps)(Login)

export default Login